package com.excel;

/**
 * @Package: com.excel
 * @ClassName: EasyExcelWiter
 * @Author: wulongkai
 * @CreateTime: 2023/10/30 11:14
 * @Description:
 */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;

import java.util.*;

public class easyExcelWriter3 {
    public static void main(String args[]) {
        // 文件输出位置
        String fileName = "/Users/a58/Desktop/test.xlsx";
        ExcelWriterBuilder builder = EasyExcel.write(fileName);
        List<Object> data = new ArrayList<>();
        List<List<String>> header = head(data);
        System.out.println(header);
        System.out.println(data.toString());
        List<List<Object>> list = new ArrayList<>();
//        list.add(data);
//        list.add(data);
        builder.head(getHeader2()).sheet("模板").doWrite(list);

    }

    /**
     * 返回的数据结构如下
     * {"data":{"xxxxxxx":{"":32,"1011":42,"1010":8,"1008":52,"0006":3,"0004":86,"1004":2,"0013":7,"0002":7,"1003":4,"0003":56,"1002":18,"0011":13,"1001":12,"0012":6,"0001":66,"1009":42}},
     * "deviceTypeSet":{"switching":["1008","1004","1003","1002","1001","1011","1010"],
     * "analog":["0006","0004","0013","0002","0003","0011","0012","0001"],
     * "switchingOff":["1009"],"substation":[""]},
     * "success":true}
     *
     * @return
     */
    private static List<List<String>> head(List<Object> data) {
        List<List<String>> headerInfo = new ArrayList<List<String>>();
        Map<String, List<String>> map = getHeader();
        Map<String, Long> dataMap = getData();
        map.forEach((k, v) -> {
            String deviceCategory = k;
            List<String> ls = v;
            ls.forEach(e -> {
                List<String> head = new ArrayList<>();
                head.add(deviceCategory);
                head.add(e);
                headerInfo.add(head);
            });
        });
        List<String> headn = new ArrayList<String>();
        headn.add("调整说明");
        headerInfo.add(headn);
        data.add(dataMap.get("合计"));
        return headerInfo;
    }

    private static Map<String, Long> getData() {
        //{"10017904-1":
        //{"":32,"1011":42,"1010":8,"1008":52,"0006":3,"0004":86,"1004":2,"0013":7,"0002":7,"1003":4,"0003":56,"1002":18,"0011":13,"1001":12,"0012":6,"0001":66,"1009":42}},
        Map<String, Long> data = new HashMap<>();
        long atotal = 0;
        long stotal = 0;
        long subtotal = 0;
        for (int i = 0; i < 10; i++) {
            String column = "温度" + i;
            atotal += Math.round(Math.random()) + i;
            data.put(column, Math.round(Math.random()) + i);
            String scolumn = "风门" + i;
            stotal += Math.round(Math.random()) + i;
            data.put(scolumn, Math.round(Math.random()) + i);
        }
        data.put("小计1", atotal);
        data.put("小计2", stotal);
        String subColumn = "其它";
        data.put(subColumn, 55l);
        data.put("小计3", 55l);
        data.put("合计", 55 + atotal + stotal);
        return data;
    }

    private static List<List<String>> getHeader2() {
        /**
         * 打算展示成如下样子
         * |客户：xxx 公司 (这一行需要合并单元格)
         * |单号: SO22222222222222|  日期: 2020-01-01 (分别需要合并单元格)
         * |产品ID|产品名称|价格|数量|总金额|备注|
         */
        String customer = "客户: ";
        String sheetNo = "单号: ";
        String dateStr = "日期: ";
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add(customer);
        head0.add(sheetNo);
        head0.add("产品ID");
        List<String> head1 = new ArrayList<String>();
        head1.add(customer);
        head1.add(sheetNo);
        head1.add("产品名称");
        List<String> head2 = new ArrayList<String>();
        head2.add(customer);
        head2.add(sheetNo);
        head2.add("价格");

        List<String> head3 = new ArrayList<String>();
        head3.add(customer);
        head3.add(sheetNo);
        head3.add("数量");

        List<String> head4 = new ArrayList<String>();
        head4.add(customer);
        head4.add(dateStr);
        head4.add("总金额");

        List<String> head5 = new ArrayList<String>();
        head5.add(customer);
        head5.add(dateStr);
        head5.add("备注");

        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);


        customer = customer + "01";
        List<String> head02 = new ArrayList<String>();
        head02.add(customer);
        head02.add(sheetNo);
        head02.add("产品ID");
        List<String> head12 = new ArrayList<String>();
        head12.add(customer);
        head12.add(sheetNo);
        head12.add("产品名称");
        List<String> head22 = new ArrayList<String>();
        head22.add(customer);
        head22.add(sheetNo);
        head22.add("价格");

        List<String> head32 = new ArrayList<String>();
        head32.add(customer);
        head32.add(sheetNo);
        head32.add("数量");

        List<String> head42 = new ArrayList<String>();
        head42.add(customer);
        head42.add(dateStr);
        head42.add("总金额");

        List<String> head52 = new ArrayList<String>();
        head52.add(customer);
        head52.add(dateStr);
        head52.add("备注");

        list.add(head02);
        list.add(head12);
        list.add(head22);
        list.add(head32);
        list.add(head42);
        list.add(head52);
        return list;
    }

    private static Map<String, List<String>> getHeader() {
        Map<String, List<String>> innerMap1 = new LinkedHashMap<>();
        List<String> aList = new ArrayList<>();
        aList.add("编制部门");
        aList.add("编制产品线");
        aList.add("职责");
        aList.add("费用属性");
        aList.add("城市");
        innerMap1.put("标签统计维度", aList);

        List<String> bList = new ArrayList<>();
        bList.add("初始维护人");
        bList.add("上一维护人");
        innerMap1.put("维护信息", bList);

        List<String> cList = new ArrayList<>();
        cList.add("审核状态");
        cList.add("标签来源");
        cList.add("数据来源");
        innerMap1.put("标签状态", cList);

        List<String> dList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dList.add("2023-0" + i);
        }
        innerMap1.put("过往数据", dList);

        List<String> eList = new ArrayList<>();
        eList.add("2023-10");
        innerMap1.put("当月编制", eList);

        List<String> fList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            fList.add("2023-0" + i);
        }
        innerMap1.put("滚动预测编制数", fList);
        return innerMap1;
    }
}


